package collectionsx

/*-*- coding:utf-8 -*-
* @Author  : ygree 
* @Time    : 2021/7/4 4:03
* @Software : IntelliJ IDEA
*/


class Setx {
    fun method() {
        println("====method====")

        val set = setOf(1, 2, 3, 4)//不允许重复
        val toMutableSet = set.toMutableSet()
        var minus = fun(it: Int): String { return it.toString() }
        println("elementAt       ${set.elementAt(2)}")
        println("elementAtOrNull ${set.elementAtOrNull(3)}")
        println("elementAtOrNull ${set.elementAtOrNull(3)}")
        println("elementAtOrElse ${set.elementAtOrElse(3, { 100 })}")
        println("minus          ${set.minus(minus)}")
        println("minusElement   ${set.minusElement(minus)}")
        println("plus           ${set.plus(1)}")
        println("plusElement    ${set.plusElement(4)}")
    }

    fun mutable() {
        println("====mutable====")
        val of = setOf(1, 2, 3)
        val plus = of.plus(4)
        println("old $of")
        println("new $plus")

        val toMutableSet = of.toMutableSet()
        val plusElement = toMutableSet.plusElement(5)
        println("old     $toMutableSet")
        println("new     $plusElement")
        println("old add ${toMutableSet.add(6)}")
        println("old     $toMutableSet")
        println("new     $plusElement")
    }

    fun asParam() {
        println("====asParam====")
        val of = setOf(1, 2, 3).toMutableSet()
        var ff = fun(set: MutableSet<Int>) {
            set.add(10)
        }
        println("hash: ${of}")
        ff(of)
        println("hash: ${of}")
    }
}

private  fun main() {
    val setx = Setx()
    setx.method()
    setx.mutable()
    setx.asParam()
}